<template>

      <div class="goods-list">
        <div class="goods-item" v-for="item in goods" :key="item.id" @click="toDetails(item.id)">
            <img :src="item.img_url" alt=""/>
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>
        
         <mt-button type="danger" size="large" @click="loadMore()">加载更多</mt-button>
    </div>

</template>

<script>
import {Toast} from "mint-ui"
export default {
    data(){
        return {
            pageNum:1,
            goods:[],
            totalPage:0

        }
    },
    methods:{
        getGoods(){
            this.$ajax({
                url:"/goods/"+this.pageNum,
                method:"get"
            }).then(resp=>{
                if(this.pageNum == 1){
                    this.goods = resp.data.message;
                }else{
                    this.goods.push(...resp.data.message);
                }
               
                this.totalPage = resp.data.totalPage;

            });
        },
        loadMore(){
             this.pageNum++;
            if(this.pageNum > this.totalPage){
                return Toast("亲，已经没有商品了。。。");

            }
            this.getGoods();//加载更多是当前页以及下一页的数据都要显示出来的
        },
        toDetails(id){
            //this.$route.params.id  参数对象 /home/:id
            //this.$router 导航对象
            this.$router.push("/home/goodsInfo");
            //push  字符串，跟的是路由地址链接
            //{}
        // this.$router.push({path:"/home/goodsInfo?id="+id});//这种非动态路由
        //this.$router.push({path:"/home/goodsInfo",query:{id}});
        //如果使用path对象两种方式：拼接路径，；要不和query一块使用，但是不能和params一块使用
        //params 和name一块使用  name指的是组件的别名
        this.$router.push({name:"goodsInfo",params:{id}});


        }

    },
    created(){
        this.getGoods();

    }

}
</script>

<style scoped>
.goods-list{
    display: flex;
    flex-wrap: wrap;
    padding:7px;
    justify-content: space-between;
}
.goods-item{
    width:49%;
    border:1px solid #ccc;
    margin:4px 0;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 293px;

}
.goods-list img{
    width: 100%;
}
.title{
    text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
    font-size: 12px;
}
.info{
    background-color: #eee;
}
.info .new{
    color:red;
    font-weight: bold;
    font-size:16px;

}
.info .old{
    text-decoration: line-through;
    margin-left:10px;
    font-size:12px;
}
.sell{
    display: flex;
    justify-content:space-between ;
    font-size:13px;
}

</style>